package com.lt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * @author lucine
 * @version 1.0 2021/11/7 20:57
 */
@Configuration
public class RedisListenerConfig {

    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 事件以__keyevent@<db>__为前缀进行发布
        container.addMessageListener(new RedisKeyExpirationListener(container), new PatternTopic("__keyevent@0__" +
                ":expired"));
        return container;
    }
}
